<?php

namespace Apps\AppApi\Method\Note;

use Apps\AppApi\Method\BaseMethod;
use PhalconPlus\Api\Common\ResultStatus;
use Apps\Common\Models\AccountBook;


/**
 * 记账本图表
 * @author      545
 * @date        2016年3月4日
 * @version     1.1.2
 * @copyright   Copyright 2015 meelier.com
 */
class NoteChart extends BaseMethod
{
    
    private $data = [];

    public function run()
    {
        //验证是否登录
        if(false == $this->verifyUserAuth()){
            return false;
        }
        
        $charttype = $this->getDataItem("charttype",1);
        $timetype = $this->getDataItem('timetype',1);
        $page = $this->getDataItem('page',1);
        
        $limit = $this->getConfig()->limit;
        $offset = ($page-1)*$limit;
        
        $userid = $this->getUserAuth()->userId;
    	//获取记一笔列表
    	
        switch ($timetype){
            case 1: 
                $where[] = "to_days(Apps\Common\Models\AccountBook.addtime) = to_days(now())";break;
            case 2:
                $where[] = "YEARWEEK(date_format(Apps\Common\Models\AccountBook.addtime,'%Y-%m-%d')) = YEARWEEK(now())";break;
            case 3:
                $where[] = "date_format(Apps\Common\Models\AccountBook.addtime,'%Y%m') = date_format(curdate(),'%Y%m')";break;
            case 4:
                $where[] = "YEAR(Apps\Common\Models\AccountBook.addtime)=YEAR(NOW())";break;
        }
        
        $str = implode(' AND ', $where);
        $str .= " AND Apps\Common\Models\AccountBook.user_id = {$userid}";
        switch ($charttype){//判断图表类型
            case 1:
                $str.= " AND ab_type = 1 ";
                $temp = AccountBook::query()
                ->columns(['SUM(ab_cashnum) as cashsum,SUM(ab_cardnum) as cardsum'])
                ->where($str)
                ->execute()->toArray();
                if ($temp){//此处仅仅是为了移动端方便，给他们设置统一的返回值 和对值进行倒序
                    if ($temp[0]['cardsum']>$temp[0]['cashsum']){
                        $this->data[0]['title'] = "刷卡支付";
                        $this->data[0]['sum'] = $temp[0]['cardsum'];
                        $this->data[1]['title'] = "现金收入";
                        $this->data[1]['sum'] = $temp[0]['cashsum'];
                    }
                    else{
                        $this->data[0]['title'] = "现金收入";
                        $this->data[0]['sum'] = $temp[0]['cashsum'];
                        $this->data[1]['title'] = "刷卡支付";
                        $this->data[1]['sum'] = $temp[0]['cardsum'];
                    }
                }
                break;
            case 2://收入分类
                $str .= " AND ab_type = 1 AND ab_innoroutid IS NOT NULL GROUP BY ab_innoroutid ";
                $this->data = AccountBook::query()
                ->columns(['i.inorout_title as title,SUM(ab_cashnum)+SUM(ab_cardnum) AS sum'])
                ->leftJoin('Apps\Common\Models\Inorout','Apps\Common\Models\AccountBook.ab_innoroutid=i.inorout_id','i')
                ->where($str)
                ->limit($limit,$offset)
                ->orderBy("sum desc")
                ->execute()->toArray();break;
            case 3://支出分类
                $str .= " AND ab_type = 2 AND ab_innoroutid IS NOT NULL GROUP BY ab_innoroutid ";
                $this->data = AccountBook::query()
                ->columns(['i.inorout_title as title,SUM(ab_cashnum)+SUM(ab_cardnum) AS sum'])
                ->leftJoin('Apps\Common\Models\Inorout','Apps\Common\Models\AccountBook.ab_innoroutid=i.inorout_id','i')
                ->where($str)
                ->orderBy("sum desc")
                ->limit($limit,$offset)
                ->execute()->toArray();break;
            case 4://会员收入
                $str .= " AND ab_type = 1 AND ab_memberid IS NOT NULL GROUP BY ab_memberid ";
                $this->data = AccountBook::query()
                ->columns(['m.member_name as title,SUM(ab_cashnum)+SUM(ab_cardnum) AS sum'])
                ->leftJoin('Apps\Common\Models\Members','Apps\Common\Models\AccountBook.ab_memberid=m.member_id','m')
                ->where($str)
                ->orderBy("sum desc")
                ->limit($limit,$offset)
                ->execute()->toArray();break;
            case 5://会员欠款
                $str .= " AND ab_type = 1 AND ab_memberid IS NOT NULL GROUP BY ab_memberid";
                $this->data = AccountBook::query()
                ->columns(['m.member_name as title,SUM(ab_arrears) AS sum'])
                ->leftJoin('Apps\Common\Models\Members','Apps\Common\Models\AccountBook.ab_memberid=m.member_id','m')
                ->where($str)
                ->orderBy("sum desc")
                ->limit($limit,$offset)
                ->execute()->toArray();break;
        }
        $this->data = $this->data==null?[]:$this->data;
        
        $this->setResult($this->data);
    }

}